﻿

using IotClientService.Distincts;
using IotClientService.Models.Hbc;
using IotContract.Dtos.Clients.Equips;
using IotContract.Dtos.Servers;
using IotContract.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XT.Common.Extensions;

namespace IotClientService.Extensions;
public static class HbcExtension
{
    public static string PRI_NGCODE = "01";
    private static string CLIENT_IMS_ID = "IMS";



    public static string[] RunFileName = new string[101]
{
    "eECS1F01", "eOCV1F01", "eOCV1F02", "eOCV1F03", "eOCV1F04", "eOCV1F05", "eOCV1F06", "eOCV1F07", "eOCV1F08", "eOCV1F09",
    "eOCV1F10", "eOCV1F11", "eOCV1F12", "eOCV1F13", "eOCV1F14", "eOCV1F15", "eOCV1F16", "eOCV1F17", "eOCV1F18", "eOCV1F19",
    "eOCV1F20", "eOCV1F21", "eOCV1F22", "eOCV1F23", "eOCV1F24", "eOCV1F25", "eFMT1F01", "eFMT1F02", "eFMT1F03", "eFMT1F04",
    "eFMT1F05", "eFMT1F06", "eFMT1F07", "eFMT1F08", "eFMT1F09", "eFMT1F10", "eFMT1F11", "eFMT1F12", "eFMT1F13", "eFMT1F14",
    "eFMT1F15", "eFMT1F16", "eFMT1F17", "eFMT1F18", "eFMT1F19", "eFMT1F20", "eFMT1F21", "eFMT1F22", "eFMT1F23", "eFMT1F24",
    "eSEL1F01", "eSEL1F02", "eSEL1F03", "eSEL1F04", "eSEL1F05", "eSEL1F06", "eSEL1F07", "eSEL1F08", "eSEL1F09", "eSEL1F10",
    "eSEL1F11", "eSEL1F12", "eSEL1F13", "eSEL1F14", "eSEL1F15", "eSEL1F16", "eSEL1F17", "eSEL1F18", "eSEL1F19", "eSEL1F20",
    "eSEL1F21", "eSEL1F22", "eSEL1F23", "eSEL1F24", "eSEL1F25", "eSEL1F26", "eSEL1F27", "eSEL1F28", "eSEL1F29", "eSEL1F30",
    "eSEL1F31", "eSEL1F32", "eSEL1F33", "eSEL1F34", "eSEL1F35", "eSEL1F36", "eSEL1F37", "eRNK1F01", "eRNK1F02", "eRNK1F03",
    "eRNK1F04", "eRNK1F05", "eRNK1F06", "eCPK1F01", "eCPK1F02", "eCPK1F03", "eCPK1F04", "eCPK1F05", "eCPK1F06", "eAWS1F01",
    "eAWS1F02"
};
    public static string[] MC_ST_LIST = new string[101]
{
    "000", "450", "451", "452", "453", "454", "455", "456", "457", "458",
    "459", "450", "461", "462", "463", "464", "465", "466", "467", "468",
    "469", "470", "471", "472", "473", "474", "000", "000", "000", "000",
    "000", "000", "000", "000", "000", "000", "000", "000", "000", "000",
    "000", "000", "000", "000", "000", "000", "000", "000", "000", "000",
    "401", "402", "403", "404", "405", "406", "407", "408", "409", "410",
    "411", "412", "413", "414", "415", "416", "417", "418", "419", "420",
    "421", "422", "423", "424", "425", "426", "427", "428", "429", "430",
    "431", "432", "433", "434", "435", "436", "437", "480", "481", "482",
    "485", "486", "487", "440", "441", "442", "445", "446", "447", "498",
    "499"
};



    public static string EquipNameToStageNo(string _equipno)
    {
        string result = "000";
        for (int i = 0; i < RunFileName.Length; i++)
        {
            if (RunFileName[i] == _equipno)
            {
                result = MC_ST_LIST[i].Trim();
                break;
            }
        }
        return result;
    }
    /// <summary>
    /// 转换为readword
    /// </summary>
    /// <param name="plcGroupNumber"></param>
    /// <param name="tags"></param>
    /// <returns></returns>
    public static List<PlcReadWord> GetReadWordInfoListByPlcGroupNumber(List<EquipTagDto> tags)
    {
        List<PlcReadWord> readWordInfos = new List<PlcReadWord>();

        tags.ForEach((EquipTagDto plcTag) => {

            int readWordCount = (plcTag.DataType != (int)PlcDataType.BIT ? (int)plcTag.DataLength : 1);
            PlcReadWord addReadWordInfo = new PlcReadWord(plcTag.AddrType, plcTag.Address.To<int>(), (PlcDataType)plcTag.DataType, readWordCount, plcTag.ReadBlock.To<int>());
            readWordInfos.Add(addReadWordInfo);
        });
        readWordInfos = readWordInfos.Distinct<PlcReadWord>(new PlcReadWordDistinct()).ToList<PlcReadWord>();
        return readWordInfos;
    }

    public static string Lf_Copy(string str, int start, int cutsize)
    {
        string text = "";
        int num = start - 1;
        text = str.Substring(num, str.Length - num);
        if (cutsize >= text.Length)
        {
            return text;
        }
        return text.Substring(0, cutsize);
    }

    public static int Lf_StrToIntDef(string s, int @default)
    {
        if (int.TryParse(s, out var result))
        {
            return result;
        }
        return @default;
    }

    /// <summary>
    /// 构建消息体,杭可设备，备注是线体
    /// </summary>
    /// <param name="group"></param>
    /// <param name="bdata"></param>
    /// <param name="rscommand"></param>
    /// <param name="retcode"></param>

    public static string GenerateHeaderMessage(HkOcvFmtEquipDto group, string bdata, string rscommand, string retcode)
    {
        string sdata = "@";
        // Line
        sdata = string.Concat(sdata, group.LineNo.Trim().PadRight(4, ' '));
        //发信人  IMS
        sdata = string.Concat(sdata, CLIENT_IMS_ID.Trim().PadRight(8, ' '));
        //收信人  eFMT1F01
        sdata = string.Concat(sdata, group.Name.Trim().PadRight(8, ' '));
        //Command  0002
        sdata = string.Concat(sdata, string.Format("{0:0000}", rscommand));
        //发送时间  
        sdata = string.Concat(sdata, string.Format("{0:yyyyMMddHHmmss}", DateTime.Now));
        //Result Code    0:OK or Good
        sdata = string.Concat(sdata, retcode.PadLeft(4, ' '));
        //Data Length
        sdata = string.Concat(sdata, string.Format("{0:000000}", bdata.Length));
        //Head End :
        sdata = string.Concat(sdata, ":");
        //Message
        sdata = string.Concat(sdata, bdata);
        //Message End
        sdata = string.Concat(sdata, "*;");
        return sdata;
    }

}
